#!/bin/sh


info_tmp="/tmp/.info_$USER_$$"
key_file="$2"

test -n "$key_file" || exit 2
localLang=$(echo ${LANG}|sed 's|\..*||g')
test -z "$localLang" &&  localLang=$(echo ${LANGUAGE}|sed 's|\..*||g')

shelldir="`dirname $0`"
export PATH="${shelldir}:$PATH"

if aapt dump badging $1 > ${info_tmp} ; then

{
    echo [ApkInfo]
    grep ^package: ${info_tmp} |sed -e "s|'||g;s|.*:[\t ]\+||g;s|[ \t]\+|\n|g" 
    grep ^application-label: ${info_tmp} |sed "s|:[\t ]*'|=|g;s|'||g"
    test -n "$localLang" && grep "^application-label-${localLang}": ${info_tmp} |sed "s|:[\t ]*'|=|g;s|'||g"
    grep '^application-icon-[a-zA-Z0-9]*:' ${info_tmp} |sed "s|:[\t ]*'|=|g;s|'||g"
    grep application: ${info_tmp} |sed "s|.*:||g;s|[ \t]\+label=| application-label=|g;;s|[ \t]\+icon=| application-icon=|g; s|[ \t]\+application-|\napplication-|g;s|'||g"
} > "$key_file"

bigicon=$(grep application-icon- "$key_file" |tail -n1|sed 's|.*=||g')
test -n "$bigicon"  && sed -i "s|application-icon=.*|application-icon=$bigicon|g" "$key_file"

test -n "$localLang" && sed -i "s|^application-label-${localLang}|application-label[${localLang}]|g" "$key_file"

else
    exit 3
fi

rm -rf ${info_tmp}
